package com.ikun.lease.web.admin.service.impl;


import com.ikun.lease.common.constant.RedisConstant;
import com.ikun.lease.model.enums.ItemType;
import com.ikun.lease.web.admin.vo.attr.AttrValueVo;
import com.ikun.lease.web.admin.vo.graph.GraphVo;
import com.ikun.lease.web.admin.vo.room.RoomDetailVo;
import com.ikun.lease.web.admin.vo.room.RoomItemVo;
import com.ikun.lease.web.admin.vo.room.RoomQueryVo;
import com.ikun.lease.web.admin.vo.room.RoomSubmitVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ikun.lease.model.entity.*;
import com.ikun.lease.web.admin.mapper.*;
import com.ikun.lease.web.admin.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【room_info(房间信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class RoomInfoServiceImpl extends ServiceImpl<RoomInfoMapper, RoomInfo>
        implements RoomInfoService {

    @Autowired
    private GraphInfoService graphService;
    @Autowired
    private RoomAttrValueService attrValueService;
    @Autowired
    private RoomFacilityService facilityService;
    @Autowired
    private RoomLabelService labelService;
    @Autowired
    private RoomPaymentTypeService paymentTypeService;
    @Autowired
    private RoomLeaseTermService leaseTermService;
    @Resource
    private RoomInfoMapper roomInfoMapper;
    @Resource
    private ApartmentInfoMapper apartmentInfoMapper;
    @Resource
    private GraphInfoMapper graphInfoMapper;
    @Resource
    private FacilityInfoMapper facilityInfoMapper;
    @Resource
    private LabelInfoMapper labelInfoMapper;
    @Resource
    private AttrValueMapper attrValueMapper;
    @Resource
    private PaymentTypeMapper paymentTypeMapper;
    @Resource
    private LeaseTermMapper leaseTermMapper;

    @Autowired
    private RedisTemplate<String,Object> redisTemplate;
    @Override
    public void saveOrUpdateRoom(RoomSubmitVo roomSubmitVo) {
        boolean b = roomSubmitVo.getId()!=null;
        super.saveOrUpdate(roomSubmitVo);
        if(b){
            //1.删除原有Graphlist
            LambdaQueryWrapper<GraphInfo> graphInfoWrapper = new LambdaQueryWrapper<>();
            graphInfoWrapper.eq(GraphInfo::getItemId,roomSubmitVo.getId());
            graphInfoWrapper.eq(GraphInfo::getItemType, ItemType.ROOM);
            graphService.remove(graphInfoWrapper);
            //2.删除原有attrValue
            LambdaQueryWrapper<RoomAttrValue> attrValueWrapper = new LambdaQueryWrapper<>();
            attrValueWrapper.eq(RoomAttrValue::getRoomId,roomSubmitVo.getId());
            attrValueService.remove(attrValueWrapper);
            //3.删除原有facility
            LambdaQueryWrapper<RoomFacility> facilityWrapper = new LambdaQueryWrapper<>();
            facilityWrapper.eq(RoomFacility::getRoomId,roomSubmitVo.getId());
            facilityService.remove(facilityWrapper);
            //4.删除原有labelInfo
            LambdaQueryWrapper<RoomLabel> labelQueryWrapper = new LambdaQueryWrapper<>();
            labelQueryWrapper.eq(RoomLabel::getRoomId,roomSubmitVo.getId());
            labelService.remove(labelQueryWrapper);
            //5.删除原有paymentTypeIds
            LambdaQueryWrapper<RoomPaymentType> paymentTypeWrapper = new LambdaQueryWrapper<>();
            paymentTypeWrapper.eq(RoomPaymentType::getRoomId,roomSubmitVo.getId());
            paymentTypeService.remove(paymentTypeWrapper);
            //6.删除原有leaseTermIds;
            LambdaQueryWrapper<RoomLeaseTerm> leaseTermWrapper = new LambdaQueryWrapper<>();
            leaseTermWrapper.eq(RoomLeaseTerm::getRoomId,roomSubmitVo.getId());
            leaseTermService.remove(leaseTermWrapper);
            //删除缓存
            redisTemplate.delete(RedisConstant.APP_ROOM_PREFIX + roomSubmitVo.getId());
        }
        //1.保存GraphList新数据
        List<GraphVo> graphVoList = roomSubmitVo.getGraphVoList();
        if (!CollectionUtils.isEmpty(graphVoList)) {
            ArrayList<GraphInfo> graphInfoList = new ArrayList<>();
            for (GraphVo graphVo : graphVoList) {
                GraphInfo graphInfo = new GraphInfo();
                graphInfo.setItemType(ItemType.ROOM);
                graphInfo.setItemId(roomSubmitVo.getId());
                graphInfo.setName(graphVo.getName());
                graphInfo.setUrl(graphVo.getUrl());
                graphInfoList.add(graphInfo);
            }
            graphService.saveBatch(graphInfoList);
        }

        //2.保存attrValue新数据
        List<Long> attrValue = roomSubmitVo.getAttrValueIds();
        if(!CollectionUtils.isEmpty(attrValue)){
            ArrayList<RoomAttrValue> arrayList = new ArrayList<>();
            for (Long aLong : attrValue) {
                RoomAttrValue roomAttrValue = RoomAttrValue.builder().roomId(roomSubmitVo.getId()).attrValueId(aLong).build();
                arrayList.add(roomAttrValue);
            }
            attrValueService.saveBatch(arrayList);
        }
        //3.保存facility新数据
        List<Long> facilitylist = roomSubmitVo.getFacilityInfoIds();
        if(!CollectionUtils.isEmpty(facilitylist)){
            ArrayList<RoomFacility> facilities = new ArrayList<>();
            for (Long aLong : facilitylist) {
                RoomFacility roomFacility = RoomFacility.builder().roomId(roomSubmitVo.getId()).facilityId(aLong).build();
                facilities.add(roomFacility);
            }
            facilityService.saveBatch(facilities);
        }
        //4.保存labelInfo新数据
        List<Long> labelList = roomSubmitVo.getLabelInfoIds();
        if(!CollectionUtils.isEmpty(labelList)){
            ArrayList<RoomLabel> labelArrayList = new ArrayList<>();
            for (Long aLong : labelList) {
                RoomLabel roomLabel = RoomLabel.builder().roomId(roomSubmitVo.getId()).labelId(aLong).build();
                labelArrayList.add(roomLabel);
            }
            labelService.saveBatch(labelArrayList);
        }
        //5.保存paymentTypeIds新数据
        List<Long> paymentlist = roomSubmitVo.getPaymentTypeIds();
        if(!CollectionUtils.isEmpty(paymentlist)){
            ArrayList<RoomPaymentType> paymentTypes = new ArrayList<>();
            for (Long aLong : paymentlist) {
                RoomPaymentType roomPaymentType = RoomPaymentType.builder().roomId(roomSubmitVo.getId()).paymentTypeId(aLong).build();
                paymentTypes.add(roomPaymentType);
            }
            paymentTypeService.saveBatch(paymentTypes);
        }
        //5.保存leaseTermIds新数据
        List<Long> leaseList = roomSubmitVo.getLeaseTermIds();
        if(!CollectionUtils.isEmpty(leaseList)){
            ArrayList<RoomLeaseTerm> leaseTerms = new ArrayList<>();
            for (Long aLong : leaseList) {
                RoomLeaseTerm roomLeaseTerm = RoomLeaseTerm.builder().roomId(roomSubmitVo.getId()).leaseTermId(aLong).build();
                leaseTerms.add(roomLeaseTerm);
            }
            leaseTermService.saveBatch(leaseTerms);
        }
    }

    @Override
    public IPage<RoomItemVo> pageroomment(IPage<RoomItemVo> page, RoomQueryVo queryVo) {
        return roomInfoMapper.pagerooment(page,queryVo);
    }

    @Override
    public RoomDetailVo getRoomDetailById(Long id) {
        //1.查询RoomInfo
        RoomInfo roomInfo = roomInfoMapper.selectById(id);
        //2.查询所属公寓信息
        ApartmentInfo apartmentInfo = apartmentInfoMapper.selectById(roomInfo.getApartmentId());
        //3.查询graphInfoList
        List<GraphVo> graphInfo = graphInfoMapper.selectListByItemTypeAndId(ItemType.ROOM,id);
        //4.查询attrValueList
        List<AttrValueVo> attrValue = attrValueMapper.selectListByItemTypeAndId(id);
        //5.查询facilityInfoList
        List <FacilityInfo> facilityInfo = facilityInfoMapper.selectListByItemTypeAndId(id);
        //6.查询labelInfoList
        List <LabelInfo> labelInfo = labelInfoMapper.selectListByItemTypeAndId(id);
        //7.查询paymentTypeList
        List <PaymentType> paymentType = paymentTypeMapper.selectListByItemTypeAndId(id);
        //8.查询leaseTermList
        List <LeaseTerm> leaseTerm = leaseTermMapper.selectListByItemTypeAndId(id);

        RoomDetailVo roomDetailVo = new RoomDetailVo();
        BeanUtils.copyProperties(roomInfo,roomDetailVo);
        roomDetailVo.setApartmentInfo(apartmentInfo);
        roomDetailVo.setAttrValueVoList(attrValue);
        roomDetailVo.setFacilityInfoList(facilityInfo);
        roomDetailVo.setLabelInfoList(labelInfo);
        roomDetailVo.setGraphVoList(graphInfo);
        roomDetailVo.setLeaseTermList(leaseTerm);
        roomDetailVo.setPaymentTypeList(paymentType);

        return roomDetailVo;

    }

    @Override
    public void removeRoomById(Long id) {
        super.removeById(id);

        LambdaQueryWrapper<GraphInfo> graphQueryWrapper = new LambdaQueryWrapper<>();
        graphQueryWrapper.eq(GraphInfo::getItemType, ItemType.ROOM);
        graphQueryWrapper.eq(GraphInfo::getItemId, id);
        graphService.remove(graphQueryWrapper);

        LambdaQueryWrapper<RoomAttrValue> attrValueLambdaQueryWrapper = new LambdaQueryWrapper<>();
        attrValueLambdaQueryWrapper.eq(RoomAttrValue::getRoomId,id);
        attrValueService.remove(attrValueLambdaQueryWrapper);

        LambdaQueryWrapper<RoomLabel> labelLambdaQueryWrapper = new LambdaQueryWrapper<>();
        labelLambdaQueryWrapper.eq(RoomLabel::getRoomId,id);
        labelService.remove(labelLambdaQueryWrapper);

        LambdaQueryWrapper<RoomFacility> facilityLambdaQueryWrapper = new LambdaQueryWrapper<>();
        facilityLambdaQueryWrapper.eq(RoomFacility::getRoomId,id);
        facilityService.remove(facilityLambdaQueryWrapper);

        LambdaQueryWrapper<RoomLeaseTerm> leaseTermLambdaQueryWrapper = new LambdaQueryWrapper<>();
        leaseTermLambdaQueryWrapper.eq(RoomLeaseTerm::getRoomId,id);
        leaseTermService.remove(leaseTermLambdaQueryWrapper);

        LambdaQueryWrapper<RoomPaymentType> paymentTypeLambdaQueryWrapper = new LambdaQueryWrapper<>();
        paymentTypeLambdaQueryWrapper.eq(RoomPaymentType::getRoomId,id);
        paymentTypeService.remove(paymentTypeLambdaQueryWrapper);

        redisTemplate.delete(RedisConstant.APP_ROOM_PREFIX + id);
    }
}




